﻿///<reference path="../../ExtJs/vswd-ext_2.2.js" />

var hidClientImgID;
var imgClientImgID;
var hidClientImgOldValue;
var ClientID;

Ext.onReady(function() {
    Ext.QuickTips.init();
    // 本地加载图片代码
    Ext.BLANK_IMAGE_URL = strPath + 'extjs/resources/images/default/s.gif';

    document.getElementById("td_maitou_image").style.height = parent.Ext.get("ContentFrame").getHeight() - 145;
    document.getElementById("img_maitou_image").style.width = (parent.Ext.get("ContentFrame").getWidth() / 2 - 80) * 0.93;
    document.getElementById("img_diaopai_image").style.width = (parent.Ext.get("ContentFrame").getWidth() / 2 - 80) * 0.93;

    ClientID = GetParastr("client_id"); //获取地址栏参数

    //图片窗口
    winClientImg = new Ext.Window({
        width: parent.Ext.get("ContentFrame").getWidth() - 60,
        height: parent.Ext.get("ContentFrame").getHeight() - 60,
        layout: 'fit',
        plain: true,
        constrain: true, // windows不超过浏览器
        autoScroll: 'true',
        closeAction: 'hide',
        modal: true,
        deferredRender: false,
        resizable: false,
        bodyStyle: 'padding:5px;',
        buttonAlign: 'center',
        contentEl: 'divClientImg',
        bodyStyle: 'border-top:0;padding:2 2 2 2;background:#E5E2EE;',
        closeAction: 'hide',
        listeners: {
            hide: function(t) {
                if (document.getElementById(hidClientImgID).value != hidClientImgOldValue)
                    UpdateClientInfo(imgClientImgID.replace('img_', ''), document.getElementById(hidClientImgID).value);
            }
        }
    });

    var httpProxyClient = new Ext.data.HttpProxy({
        url: strPath + 'GGFW/DocumentsManage/ClientCommand.fwf?Method=GetClientList'
    });

    var readerProxyClient = new Ext.data.JsonReader({
        root: 'Records',
        id: 'HWTransferItemID',
        totalProperty: 'totalCount',
        fields: [
            { name: 'client_id' }
             , { name: 'client_code' }
             , { name: 'client_name' }
             , { name: 'lianxiren' }
             , { name: 'telephone' }
             , { name: 'address' }
             , { name: 'maokuan' }
             , { name: 'maitou' }
             , { name: 'maitou_image' }
             , { name: 'diaopai' }
             , { name: 'diaopai_image' }
             , { name: 'isdeleted' }
        ]
    });

    // 数据源
    ClientStore = new Ext.data.Store({
        proxy: httpProxyClient,
        reader: readerProxyClient,
        remoteSort: true
    });

    ClientStore.load({ params: { start: 0, limit: 500, client_id: ClientID} });

    // 加载后
    ClientStore.on('load', function() {
        if (ClientStore.getTotalCount() > 0) {
            document.getElementById("ipt_client_code").value = ClientStore.getAt(0).get("client_code");
            document.getElementById("ipt_client_name").value = ClientStore.getAt(0).get("client_name");
            document.getElementById("ipt_lianxiren").value = ClientStore.getAt(0).get("lianxiren");
            document.getElementById("ipt_telephone").value = ClientStore.getAt(0).get("telephone");
            document.getElementById("ipt_address").value = ClientStore.getAt(0).get("address");
            document.getElementById("ipt_maokuan").value = ClientStore.getAt(0).get("maokuan");
            document.getElementById("ipt_maitou").value = ClientStore.getAt(0).get("maitou");
            document.getElementById("ipt_diaopai").value = ClientStore.getAt(0).get("diaopai");
            document.getElementById("hid_maitou_image").value = ClientStore.getAt(0).get("maitou_image") == null ? "" : ClientStore.getAt(0).get("maitou_image");
            document.getElementById("hid_diaopai_image").value = ClientStore.getAt(0).get("diaopai_image") == null ? "" : ClientStore.getAt(0).get("diaopai_image");
            document.getElementById("img_maitou_image").src = ClientStore.getAt(0).get("maitou_image") == null ? "" : "../UploadFile/" + ClientStore.getAt(0).get("maitou_image");
            document.getElementById("img_diaopai_image").src = ClientStore.getAt(0).get("diaopai_image") == null ? "" : "../UploadFile/" + ClientStore.getAt(0).get("diaopai_image");
        }
    });

})

//更新客户信息
function UpdateClientInfo(filedname, filedvalue) {

    var strUrl = strPath + 'GGFW/DocumentsManage/ClientCommand.fwf?Method=UpdateClientInfo';

    Ext.Ajax.request({
        url: strUrl,
        method: 'POST',
        timeout: 60000, //设定超时时间为60秒
        params: {
            ClientID: ClientID,
            FiledName: filedname,
            FiledValue: filedvalue
        },
        failure: function(response, options) {
            ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
        },
        success: function(response, options) {
            var json = response.responseText;
            var obj = parseJsonResult(json);

            if (!obj.Success) {
                showMessage("错误！", "更新出现错误！请查看详细原因。", obj.Message);
            }
        }
    });
}

//打开图片上传窗口
function showWinClientImg(imgImgID, hidImgID) {
    imgClientImgID = imgImgID;
    hidClientImgID = hidImgID;
    hidClientImgOldValue = document.getElementById(hidClientImgID).value;
    document.getElementById("imgClient").src = "../UploadFile/" + document.getElementById(hidClientImgID).value;
    document.getElementById(imgClientImgID).src = "../UploadFile/" + document.getElementById(hidClientImgID).value;
    document.getElementById("imgClient").style.height = parent.Ext.get("ContentFrame").getHeight() - 160;
    winClientImg.setTitle("图片上传更新");
    document.getElementById("span_info").innerHTML = "请选择图片文件（*.jpg,*.gif,*.png,*.bmp)";
    controlShow('divClientImg');
    winClientImg.show();
}

function CloseWinClientImg() {
    winClientImg.hide();
}